package com.project.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.MyBatisConfig;
import com.project.bean.CarBean;
import com.project.mapper.ICarMapper;
import com.project.service.ICarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;

@Service
@Transactional
public class CarServiceImpl implements ICarService {
    @Autowired
    private ICarMapper mapper;

    @Override
    public void add(CarBean car) {
        mapper.add(car);
    }

    @Override
    public void del(int id) {
        mapper.del(id);
    }

    @Override
    public void update(int id, int price) {
        mapper.update(id, price);
    }

    @Override
    public CarBean findById(int id) {
        return mapper.findById(id);
    }

    @Override
    public PageInfo<CarBean> findByItem(int pageNO, String type, Integer startPrice, Integer endPrice) {
        PageHelper.startPage(pageNO, PAGESIZE);
        PageInfo<CarBean> pageInfo = PageInfo.of(
                mapper.findByItem(type, startPrice, endPrice));

        return pageInfo;
    }

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBatisConfig.class);
        DataSource d = context.getBean(DataSource.class);
        System.out.println(d);
    }
}
